﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SampleWinProject
{
    public class Reglex
    {
        const string TableSearchKey = "<t n=\"";
        const string TimeStampKey = "ts=\"";
        const string FieldStartKey = " field=\"";
        const string RowEndKey = "</r>";

        private const string Content = "<t n=\"t1\" ts=\"12345678\" field=\"a,b,c,d,e,f\"><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r></t>" +
            "<t n=\"t2\" ts=\"12345678\" field=\"a,b,c,d,e,f\"><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r></t>" +
            "<t n=\"t3\" ts=\"12345678\" field=\"a,b,c,d,e,f\"><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r><r>1|1|1|1|1|1</r></t>";

        public static void Test()
        {
            int starttime = System.Environment.TickCount;
            for (int i = 0; i < 1000; i++)
            {
                Run();
            }
            int end = System.Environment.TickCount;
            MessageBox.Show((end - starttime).ToString());

            starttime = System.Environment.TickCount;
            for (int i = 0; i < 1000; i++)
            {
                RunReglex();
            }
            end = System.Environment.TickCount;
            MessageBox.Show((end - starttime).ToString());
        }

        public static void Run()
        {
            
            

            string FieldName = "";
            string tableName = "";
           
            try
            {
                //解析内容
                int index = -1;
                int start = 0;
       
                
                while ((index = Content.IndexOf("</t>", start)) != -1)
                {
                    string str = Content.Substring(start, index - start + 4);
                    start = index + 4;

                                     
                    int tableNameStart = str.IndexOf(TableSearchKey) + TableSearchKey.Length;
                    //获取同步表名
                    tableName = str.Substring(tableNameStart, str.IndexOf("\"", tableNameStart) - tableNameStart);

                    int FieldStart = str.IndexOf(FieldStartKey) + FieldStartKey.Length;

                    string Fields = str.Substring(FieldStart, str.IndexOf("\"", FieldStart) - FieldStart);
                    //字段列表
                    string[] FieldList = Fields.Split(',');

                    //获取同步时间戳起始位
                    int StampStart = str.IndexOf(TimeStampKey) + TimeStampKey.Length;
                    //获取同步时间戳
                    long TimeStamp = long.Parse(str.Substring(StampStart, str.IndexOf("\"", StampStart) - StampStart));
                    //清除开头和结束标记
                    str = str.Substring(str.IndexOf('>', StampStart) + 1).Replace("</t>", string.Empty);
                    str.TrimEnd('\n');

                    int startTime = System.Environment.TickCount;

                    //判断是否有同步记录
                    if (str.Length > 0)
                    {
                        //主键索引
                        System.Collections.Generic.List<int> keyindexs = new System.Collections.Generic.List<int>();
                        keyindexs.Add(0);
                        //构建Update以及Select语句
                       

                 
                        int RowStart = 0; //行开始标记
                        int RowEnd = 0;//行结束标记

                        //开始解析内容
                        while ((RowStart = str.IndexOf('<', RowEnd)) != -1)
                        {
                            int ValueStart = str.IndexOf('>', RowStart + 1) + 1;
                            int ValueEnd = str.IndexOf("</r>", RowStart);
                            //获取行结束位置
                            RowEnd = ValueEnd + "</r>".Length;
                            string[] Values = str.Substring(ValueStart, ValueEnd - ValueStart).Split('|');
                           
                            object[] objes = new object[keyindexs.Count];
                            //查找主键值
                            for (int l=0; l < keyindexs.Count; l++)
                            {
                                objes[l] = Values[keyindexs[l]];
                            }
                            
                            //result.ReadFirst
                            
                            int i = 0;
                            foreach (string Field in FieldList)
                            {
                                FieldName = Field;
                                string value = Values[i++].Replace("&gt;",">").Replace("&lt;","<");
                                object val = DBNull.Value;
                                
                            }

                              
                         
                           
                        }

                        
                    }


                }


            }
            
            catch(InvalidCastException ex)
            {
               throw ex;
            }
            catch (Exception ex)
            {
               throw new Exception("解析数据异常：" +ex.Message);
            }

           
        }

        public static void RunReglex()
        {



            string FieldName = "";
            string tableName = "";

            try
            {
                //解析内容
                int index = -1;
                int start = 0;


                while ((index = Content.IndexOf("</t>", start)) != -1)
                {
                    string str = Content.Substring(start, index - start + 4);
                    start = index + 4;


                    int tableNameStart = str.IndexOf(TableSearchKey) + TableSearchKey.Length;
                    //获取同步表名
                    tableName = str.Substring(tableNameStart, str.IndexOf("\"", tableNameStart) - tableNameStart);

                    int FieldStart = str.IndexOf(FieldStartKey) + FieldStartKey.Length;

                    string Fields = str.Substring(FieldStart, str.IndexOf("\"", FieldStart) - FieldStart);
                    //字段列表
                    string[] FieldList = Fields.Split(',');

                    //获取同步时间戳起始位
                    int StampStart = str.IndexOf(TimeStampKey) + TimeStampKey.Length;
                    //获取同步时间戳
                    long TimeStamp = long.Parse(str.Substring(StampStart, str.IndexOf("\"", StampStart) - StampStart));
                    //清除开头和结束标记
                    str = str.Substring(str.IndexOf('>', StampStart) + 1).Replace("</t>", string.Empty);
                    str.TrimEnd('\n');

                    int startTime = System.Environment.TickCount;
                    //判断是否有同步记录
                    if (str.Length > 0)
                    {
                        //主键索引
                        System.Collections.Generic.List<int> keyindexs = new System.Collections.Generic.List<int>();
                        keyindexs.Add(0);
                        //构建Update以及Select语句



                        int RowStart = 0; //行开始标记
                        int RowEnd = 0;//行结束标记

                        //开始解析内容
                        while ((RowStart = str.IndexOf('<', RowEnd)) != -1)
                        {
                            int ValueStart = str.IndexOf('>', RowStart + 1) + 1;
                            int ValueEnd = str.IndexOf("</r>", RowStart);
                            //获取行结束位置
                            RowEnd = ValueEnd + "</r>".Length;
                            string[] Values = str.Substring(ValueStart, ValueEnd - ValueStart).Split('|');

                            object[] objes = new object[keyindexs.Count];
                            //查找主键值
                            for (int l = 0; l < keyindexs.Count; l++)
                            {
                                objes[l] = Values[keyindexs[l]];
                            }

                            //result.ReadFirst

                            int i = 0;
                            foreach (string Field in FieldList)
                            {
                                FieldName = Field;
                                string value = Values[i++].Replace("&gt;", ">").Replace("&lt;", "<");
                                object val = DBNull.Value;

                            }




                        }


                    }


                }


            }

            catch (InvalidCastException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new Exception("解析数据异常：" + ex.Message);
            }

        }
    }
}
